local Msg = require("core.Msg")
local VipConfig = require("excel.Vip").vip
local detailConfig = require("excel.Vip").detail
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local VipDefine = require("vip.VipDefine")
local SmeltLogic = require("bag.SmeltLogic")
local Log = require("common.Log")
local StoreLogic = require("store.StoreLogic")
local TaskGroup = require("task.TaskGroup")
local ObjHuman = require("core.ObjHuman")
local MailLogicFL = require("mail.MailLogicFL")
local PfLogic = require("tencentApi.PfLogic")

--得到额外增加的册数，index在vipDefine中定义
function getVipTimes(human, useWord, lv, isShow)
--  if true then return 0 end
  if not lv then
    lv = getVipLv(human)
    if lv <= 0 then
      return 0
    end
  end
  local config = VipConfig[lv]
  local cnt = 0
  if not useWord or useWord == "" then
    return 0
  end
  cnt = config[useWord]
  if cnt < 0 and not isShow and useWord ~= VipDefine.VIP_PRIVILEGR_OFFLINEPKCD then
    cnt = 0
  end
  return cnt
end
--仓库开启多少个格子
function getVipStoreLen(human)
  local lv = getVipLv(human)
  local total = 0
  for i=1, #VipConfig do
    if lv >= i and VipConfig[i].openStore ~= 0 then
      total = total + 56
    end
  end
  return total
end
--检查背包格子是否要增加
function checkBagCanAdd(human, oldPay)  
  local lv = getVipLv(human)
  local oldLv = getVipLv(human, oldPay)
  if lv > oldLv then
    if lv >= 6 and oldLv <6 and PfLogic.isAgentTencent() then      
      MailLogicFL.CreateNewMailForUser(human._id, "VIP将军亲启", "    亲爱的VIP将军您好，为了帮助您更好的解决问题并且获得更多的VIP福利与物品,请您加入VIPQQ：2881060895。\n    VIP小小乔将为您竭诚服务帮助您解决您遇见的游戏内外问题。\n    加入VIP您将享受专属客服一对一，同时对游戏更新内容可提前预告等服务。", {})
    end
    oldLv = oldLv > 0 and oldLv or 1
    oldLv = oldLv + 1
    for i=oldLv, lv do
      local addtmp = getVipTimes(human, VipDefine.VIP_PRIVILEGE_ADD_CAP, (i - 1 > 1) and (i - 1) or 1)
      local addCap = getVipTimes(human, VipDefine.VIP_PRIVILEGE_ADD_CAP, i)
      if addtmp == 0 and addCap > 0 then
        BagLogic.addVipCap(human, addCap)
      end
      addtmp = getVipTimes(human, VipDefine.VIP_PRIVILEGE_OPENSTORE, (i - 1 > 1) and (i - 1) or 1)
      addCap = getVipTimes(human, VipDefine.VIP_PRIVILEGE_OPENSTORE, i)      
      if addtmp == 0 and addCap > 0 then
        StoreLogic.addVipCap(human, addCap)
      end
    end
  end
end

--充值之后设置vip信息
function setRewardCanGet(human, oldPay)
  local lv = getVipLv(human)
  local oldLv = getVipLv(human, oldPay)
  if lv ~= oldLv then
    for i=0, lv - 1 do
      if human.db.vipRewardIsGet[i] == VipDefine.REWARD_NOT_CAN_GET then
        human.db.vipRewardIsGet[i] = VipDefine.REWARD_CAN_GET
      end
    end
    sendVipInfo(human)
    checkBagCanAdd(human, oldPay)
    
    TaskGroup.onChangeVip(human)   --VIP改变，自动接取下一个日常任务
    ObjHuman.doCalc(human)    
    Log.write(Log.LOGID_OSS_VIP, human._id, human.account, human.name,human.db.lv, lv)
  end
end

function sendVipInfo(human)
  local sendMsg = Msg.gc.GC_VIP_LV_INFO
  sendMsg.list[0] = #VipConfig
  local cache = {}
  for i=1, #VipConfig do
    local len = 0
    for k,v in pairs(detailConfig) do
      len = len + 1
      sendMsg.list[i].detail[len].times = getVipTimes(human, k, i, true)
      sendMsg.list[i].detail[len].showPos = v.showPos
    end
    sendMsg.list[i].detail[0] = len
    sendMsg.list[i].reward[0] = #VipConfig[i].reward
    sendMsg.list[i].isValue[0] = #VipConfig[i].reward 
    for j=1, #VipConfig[i].reward do
      Grid.create(cache, VipConfig[i].reward[j][1], VipConfig[i].reward[j][2])
      Grid.makeItem(cache, sendMsg.list[i].reward[j], 0, human)
      sendMsg.list[i].isValue[j] = VipConfig[i].isValue[j]
    end
    sendMsg.list[i].isGet = human.db.vipRewardIsGet[i - 1]
    sendMsg.list[i].chongzhi = VipConfig[i].needYuanbao
    sendMsg.list[i].image[0] = #VipConfig[i].image
    for j=1, #VipConfig[i].image do
      sendMsg.list[i].image[j] = VipConfig[i].image[j]
    end
    sendMsg.list[i].effect = VipConfig[i].effect
    sendMsg.list[i].source = VipConfig[i].source
  end
  Msg.send(sendMsg, human.fd)
end

function setDetail(human)
  local sendMsg = Msg.gc.GC_VIP_DETAIL
  local len = 0
  for k,v in pairs(detailConfig) do
    len = len + 1
    sendMsg.info[len].detail = v.detail
    sendMsg.info[len].showPos = v.showPos
  end
  sendMsg.info[0] = len
  Msg.send(sendMsg, human.fd)
end

function openWindow(human)
  sendVipInfo(human)
end


function getReward(human, vipLv)
  if human.db.vipRewardIsGet[vipLv - 1] ~= VipDefine.REWARD_CAN_GET then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.MONTH_CARD_CANOT_GET_REWARD)
  end
  if not BagLogic.checkCanAddItemList(human, VipConfig[vipLv].reward,#VipConfig[vipLv].reward) then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.MONTH_CARD_BAG_NOT_ENOUGH)
  end
  human.db.vipRewardIsGet[vipLv - 1] = VipDefine.REWARD_ALREADY_GET
  for j=1, #VipConfig[vipLv].reward do
    BagLogic.addItem(human, VipConfig[vipLv].reward[j][1], 1, VipConfig[vipLv].reward[j][2], "monthCard")
  end  
  sendVipInfo(human)
end


function getVipLv(human, oldPay)
  local yb = oldPay or human.db.historyYuanbao 
  if yb < VipConfig[1].needYuanbao then
    return 0
  end
  if yb >= VipConfig[#VipConfig].needYuanbao then
    return #VipConfig
  end
  for i=1, #VipConfig - 1 do
    if yb >= VipConfig[i].needYuanbao and yb < VipConfig[i + 1].needYuanbao then
      return i
    end
  end
  return 0
end

function checkSendVipMail(human)
  local lv = getVipLv(human)
  --如果在更新之前登陆的号重新发一遍邮件
  if lv >= 6 and human.db.lastLogoutTime <= 1442541600 and PfLogic.isAgentTencent() then
    MailLogicFL.CreateNewMailForUser(human._id, "VIP将军亲启", "    亲爱的VIP将军您好，为了帮助您更好的解决问题并且获得更多的VIP福利与物品,请您加入VIPQQ：2881060895。\n    VIP小小乔将为您竭诚服务帮助您解决您遇见的游戏内外问题。\n    加入VIP您将享受专属客服一对一，同时对游戏更新内容可提前预告等服务。", {})
  end
end
